<template>
  <div class="student-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>学生列表</span>
        <el-button
            style="float: right; padding: 3px 0"
            type="text"
            @click="handleAdd"
        >添加学生</el-button>
      </div>

      <el-table :data="studentList" border style="width: 100%">
        <el-table-column prop="studentNo" label="学号" width="120"></el-table-column>
        <el-table-column prop="name" label="姓名" width="100"></el-table-column>
        <el-table-column prop="gender" label="性别" width="80"></el-table-column>
        <el-table-column prop="className" label="班级" width="120"></el-table-column>
        <el-table-column prop="major" label="专业"></el-table-column>
        <el-table-column prop="college" label="学院"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getStudentList, deleteStudent } from '@/api/student'

export default {
  name: 'StudentList',
  data() {
    return {
      studentList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.getStudentList()
  },
  methods: {
    getStudentList() {
      const params = {
        page: this.currentPage,
        size: this.pageSize
      }
      getStudentList(params).then(response => {
        this.studentList = response.data.list
        this.total = response.data.total
      })
    },
    handleAdd() {
      this.$router.push('/student/add')
    },
    handleEdit(student) {
      this.$router.push(`/student/edit/${student.id}`)
    },
    handleDelete(id) {
      this.$confirm('确认删除该学生吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStudent(id).then(() => {
          this.$message.success('删除成功')
          this.getStudentList()
        })
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getStudentList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getStudentList()
    }
  }
}
</script>

<style lang="scss" scoped>
.student-container {
  padding: 20px;
}
</style>